<?php

namespace app\api\controller\product;

use think\Controller;
use think\Request;

use function PHPSTORM_META\type;

class Product extends Controller {
    public function __construct()
    {
        parent::__construct();
        $this->TypeModel = model('Product.ProductType');
        $this->ProductModel = model('Product.Product');
        $this->VipModel = model('Product.ProductVip');
    }

    public function index() {
        $typeid = $this->request->param('typeid', 0);
        $flag = $this->request->param('flag', 0);
        $OrderBy = $this->request->param('OrderBy', 'id');
        $name = $this->request->param('keyword');
        $name = trim($name);
        $where['typeid'] = $typeid;

        if ($flag != 0) {
            // $where[] = ['flag' => $flag];
            $where['flag'] = $flag;
        }
        $url = '/product/product/index?typeid=' . $typeid;
        // halt($where);
        $list = $this->ProductModel->where($where)->where('name', 'LIKE', '%' . $name . '%')
        ->order($OrderBy, 'desc')->select();
        if(!$list)
        {
            $this->error('暂无数据');
        }
        $this->success('查询成功',$url,$list);
    }

    public function type() {
        $type = $this->TypeModel->order('id', 'asc')->select();

        if (!$type) {
            $this->error('查询失败');
        }
        // return $type;
        $this->success('查询成功', null, $type);
    }
}
